const api = require('../config/api.js');
/**
 * 封装登录
 */
function login() {
    return new Promise(function (resolve,reject) {
        wx.login({
            success:res => {
                if (res.code){
                    console.log(res);
                    resolve(res)
                }
            },
            fail:function (err) {
                reject(err)
            }
        })
    })
}
/**
 * 封装获取用户信息
 */
function getUserInfo() {
    return new Promise(function (resolve, reject) {
        wx.getUserInfo({
            withCredentials:true,
            success:res=>{
                console.log(res)
                resolve(res)
            },
            fail:function (err) {
                reject(err)
            }
        })
    })
}
/**
 * 检查微信会话是否过期
 * */
function checkSession() {
    return new Promise(function (resolve, reject) {
        wx.checkSession({
            success:function () {
                resolve(true)
            },
            fail:function () {
                reject(false)
            }
        })
    })
}
/**
 * 封装微信请求
 */
function request(url, data = {}, method = "GET") {
    return new Promise(function (resolve, reject) {
        wx.request({
            url: url,
            data: data,
            method: method,
            header: {
                'Content-Type': 'application/json',
                'X-Nideshop-Token': wx.getStorageSync('token')
            },
            success: function (res) {
                console.log("success");
                if (res.statusCode == 200) {
                    if (res.data.errno == 401) {
                        //需要登录后才可以操作
                        let code = null;
                        return login().then((res) => {
                            code = res.code;
                            return getUserInfo();
                        }).then((userInfo) => {
                            //登录远程服务器
                            request(api.AuthLoginByWeixin, { code: code}, 'POST').then(res => {
                                console.log(res)
                                if (res.code == 1) {
                                    //存储用户信息
                                    console.log(code)
                                    wx.setStorageSync('userInfo', res.data.userInfo);
                                    resolve(res);
                                } else {
                                    reject(res);
                                }
                            }).catch((err) => {
                                reject(err);
                            });
                        }).catch((err) => {
                            reject(err);
                        })
                    } else {
                        resolve(res.data);
                    }
                } else {
                    reject(res.errMsg);
                }

            },
            fail: function (err) {
                reject(err)
                console.log("failed")
            }
        })
    });
}

module.exports = {
    login,
    getUserInfo,
    request,
    checkSession
}